package com.example.demo.mapper;

import com.example.demo.entity.WorkLogs;
import com.example.demo.entity.WorkTable;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Mapper
@Repository(value = "RunTableMapper")
public interface RunTableMapper {

    @Insert("INSERT INTO run_table (\n" +
            "start_time,update_time,phone\n" +
            ")VALUES(\n" +
            "#{startTime},#{updateTime},#{phone}\n" +
            ")")
    void startRunTable(WorkTable workTable);




    @Update("UPDATE run_table SET earn_money = earn_money+#{earnMoney} ,run_time = run_time+#{allTime},update_time= #{updateTime}\n" +
            "WHERE phone = #{phone} AND state = 0")
    void updateMemberWork(WorkTable workTable);
//    @Update("UPDATE run_table SET earn_money = earn_money+#{earnMoney} ,run_time = run_time+#{allTime},update_time= #{updateTime}\n" +
//            "WHERE phone = #{phone} AND state = 0")
//    void updateMemberWorkLog(WorkTable workTable);

    @Update("UPDATE run_table SET earn_money = #{afterUpAmount} ,run_time = #{updateAfterDate},update_time= #{endDate}\n" +
            "WHERE phone = #{phone} AND state = 0")
    void updateMemberWorkLog(WorkLogs workLogs);


    @Update("UPDATE run_table SET state = 1,earn_money =  #{earnMoney}, \n" +
            "run_time = #{time},update_time = #{updateTime},end_time = #{endTime}\n" +
            "WHERE phone = #{phone} AND state = 0")
    void endMemberWork(WorkTable workTable);

    @Update("UPDATE run_table SET state = 1,earn_money = #{earnMoney},\n" +
            " run_time = #{allTime},update_time = #{updateTime},end_time = #{endTime}\n" +
            " WHERE phone = #{phone} AND state = 0")
    void endMemberWorkL(WorkTable workTable);

    @Update("UPDATE run_table SET state = 1,earn_money = #{afterUpAmount},\n" +
            " run_time = #{updateAfterDate},update_time = #{endDate},end_time = #{endDate}\n" +
            " WHERE phone = #{phone} AND state = 0")
    void endMemberWorkLog(WorkLogs workLogs);

    @Update("UPDATE run_table SET state = 0 ,update_time = #{updateTime} ,end_time = #{endTime} \n" +
            "WHERE id = #{id}")
    void startRunTableAgain(WorkTable workTable);

    @Select("SELECT id FROM run_table WHERE phone = #{phone} AND state = 0")
    String getRuningTable(String phone);

    @Update("UPDATE run_table SET state = 1 WHERE id = #{id}")
    void endRuningTable(String id);



    @Select("SELECT update_time FROM run_table WHERE phone = #{phone} AND state = 0   FOR UPDATE")
    Date getStartTime(String phone);

    @Select("SELECT run_time FROM run_table WHERE phone = #{phone} AND state = 0")
    Integer getUseDate(String phone);

    @Select("SELECT earn_money FROM run_table WHERE phone = #{phone} AND state = 0")
    Integer getUseMoney(String phone);

    @Select("SELECT update_time FROM run_table WHERE id = #{id}  ")
    Date getStartTime2(String id);

    @Select("SELECT id,run_time as  allTime\n" +
            "FROM run_table WHERE phone = #{phone}\n" +
            "ORDER BY start_time DESC LIMIT 1")
    WorkTable getWorkTable(String phone);

    @Select("Select id,run_time AS  allTime,start_time,earn_money,run_time AS allTime,update_time,end_time,state,update_time as updateTime2 \n" +
            "FROM run_table WHERE phone = #{phone}\n" +
            "ORDER BY start_time DESC LIMIT 1")
    WorkTable getWorkTableAll(String phone);

    @Select("SELECT end_time FROM run_table WHERE phone = #{phone}\n" +
            "ORDER BY start_time DESC LIMIT 1")
    Date getEndDate(String phone);


    @Select("select phone from run_table where id = #{id}")
    String getPhone(Integer id);
}
